package com.yiren.dbaa.module.practice_set.map.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.map.contract.MapManageContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/29 17:16
 * @description: 地图管理
 */
public class MapManageModel implements MapManageContract.Model {

    private Activity activity;
    private Gson gson;

    public MapManageModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void updateUserRange(String userId, MapEntity map, int useRange, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.updateUserRange_Map("?useRange=" + useRange + "&userId=" + userId +
                "&siteCode=" + map.getSiteCode() + "&mapVer=" + map.getMapVer()), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<String> testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<String>>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    callBack.success(testBaseBean.getData());
                } else {
                    callBack.fail("设置失败!");
                }
            }

            @Override
            public void getFail() {
                callBack.fail("数据请求失败!");
            }
        });
    }

    @Override
    public void getMapBindLines(String mapId, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.getMapBindLines("?mapId=" + mapId), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<LineInfoEntity>> testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<List<LineInfoEntity>>>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    callBack.success(testBaseBean.getData());
                } else {
                    callBack.fail("获取绑定线路失败！");
                }
            }
        });
    }

    @Override
    public void updateUserRangeLoacl(MapEntity map, int range) {
        map.setUseRange(range);
        UserDbController.getInstance(false).update(map);
    }
}
